package com.aviccxzx.smarthome.UserModule;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.entity.DoorMsg;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.gizwits.gizwifisdk.api.GizWifiDevice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.aviccxzx.smarthome.fragment.DeviceListFragment.subList;
import static com.aviccxzx.smarthome.utils.ListUtils.listToString;


/**
 * Created by joey on 2016/10/9.
 */
public class MessageCenterActivity extends GosUserModuleBaseActivity {

    List<DoorMsg.DataBean> msgList = new ArrayList<>();
    DoorAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.message_center_lay_v19);
        } else {
            setContentView(R.layout.message_center_lay);
        }
        ((TextView) findViewById(R.id.title_tv)).setText("消息中心");
        setOnclick();

        ListView msgilst = (ListView) findViewById(R.id.list_view);
        adapter = new DoorAdapter();
        msgilst.setAdapter(adapter);
        getMsg();


    }

    public void getMsg() {
        progressDialog.show();
        List<String> macList = new ArrayList<>();
        for (GizWifiDevice device : subList) {
            macList.add(device.getMacAddress());
        }
        String macs = listToString(macList);
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<DoorMsg> call = service.getDataHistory(macs);
        call.enqueue(new Callback<DoorMsg>() {
            @Override
            public void onResponse(Call<DoorMsg> call, Response<DoorMsg> response) {
                if (response.body() != null && response.body().getData() != null) {
                    int code = response.body().getCode();
                    if (code == 1) {
                        msgList = response.body().getData();
                        if (msgList != null && msgList.size() > 0) {
                            adapter.notifyDataSetChanged();
                        } else {
                            progressDialog.dismiss();
                        }
                    }else {
                        progressDialog.dismiss();
                    }
                }else {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onFailure(Call<DoorMsg> call, Throwable t) {
                progressDialog.dismiss();
            }
        });
    }


    private void setOnclick() {
        findViewById(R.id.ll_title_left_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    HashMap<Integer, View> mHashMap = new HashMap<>();

    public class DoorAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return msgList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            DoorAdapter.ViewHolder holder = null;
            if (mHashMap.get(i) == null) {
                holder = new DoorAdapter.ViewHolder();
                convertView = LayoutInflater.from(MessageCenterActivity.this).inflate(R.layout.doormsg_listitem_lay, viewGroup, false);
                mHashMap.put(i, convertView);
                convertView.setTag(holder);
            } else {
                convertView = mHashMap.get(i);
                holder = (DoorAdapter.ViewHolder) convertView.getTag();
            }

            holder.msg_name = (TextView) convertView.findViewById(R.id.msg_name);
            holder.msg_time = (TextView) convertView.findViewById(R.id.msg_time);

            DoorMsg.DataBean data = msgList.get(i);
            if (data != null) {
                for (GizWifiDevice device : subList) {
                    if (device.getMacAddress().toLowerCase().equals(data.getDevice_mac())) {
                        holder.msg_name.setText(!TextUtils.isEmpty(device.getAlias()) ? device.getAlias() + data.getState_desplay() : device.getProductName() + data.getState_desplay());
                    }
                }

                holder.msg_time.setText(data.getCtime());
            }
            progressDialog.dismiss();
            return convertView;
        }

        public class ViewHolder {
            TextView msg_name, msg_time;
        }
    }

}
